


// RingMesh.ts

import utils from "@utils";
import { Group, Mesh, TorusGeometry, Vector2 } from "three";
import Text3D from "three-base/Text3D";
import metalMaterial from "../materials/metalMaterial";


// 戒指模型
export default class RingMesh extends Group {

    constructor() {
        super();
    }

    //半径
    radius = 2;

    //3dtext
    text3d = (() => {
        const text3d = new Text3D("ring");
        // text3d.setText({});
        text3d.addEventListener("loaded", e => {
            console.log("text3d loaded");
            const positoinAttribute = text3d.geometry.getAttribute("position");
            const v2 = new Vector2();

            for (let i = 0; i < positoinAttribute.count; i++) {
                const x = positoinAttribute.getX(i);
                const z = positoinAttribute.getZ(i);
                //周长
                const L = Math.PI * Math.pow(this.radius, 2);
                const angle = (x / L) * (Math.PI / 180);

                v2.set(0, this.radius + z);
                v2.rotateAround(new Vector2(0, 0), angle * -500);
                positoinAttribute.setX(i, v2.x);
                positoinAttribute.setZ(i, v2.y);
            }
            positoinAttribute.needsUpdate = true;
        });
        text3d.material = metalMaterial;
        //three事件
        text3d.userData = {
            cursor: "pointer",
            enableEvent: true,
        };
        text3d.addEventListener("dbclick", e => {
            utils.editorMaterial(metalMaterial);
        });
        this.add(text3d);
        return text3d;
    })();

    ringMesh = (() => {
        const ringGeometry = new TorusGeometry(2, 0.08, 16, 100);
        ringGeometry.rotateX(Math.PI / 2);
        const ringMesh = new Mesh(ringGeometry, metalMaterial);
        this.add(ringMesh);
        return ringMesh;
    })();

}